package com.onesports.intelligent.k12.polarlight.controller.common;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.onesports.intelligent.k12.polarlight.common.utils.ResultUtil;
import com.onesports.intelligent.k12.polarlight.constant.ApiPathConstants;
import com.onesports.intelligent.k12.polarlight.constant.BizConstants;
import com.onesports.intelligent.k12.polarlight.domain.entity.course.CourseScheduleTemplate;
import com.onesports.intelligent.k12.polarlight.domain.entity.course.CourseSubLevel;
import com.onesports.intelligent.k12.polarlight.domain.entity.sys.Dict;
import com.onesports.intelligent.k12.polarlight.domain.vo.common.CanChoiceDataVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.common.LocationVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.course.CampusCourseVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.course.CampusScheduleVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.course.CourseClassificationVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.course.TemplateVO;
import com.onesports.intelligent.k12.polarlight.mapper.course.CourseScheduleTemplateMapper;
import com.onesports.intelligent.k12.polarlight.security.SecurityUtils;
import com.onesports.intelligent.k12.polarlight.service.campus.OrganizationCampusService;
import com.onesports.intelligent.k12.polarlight.service.cls.ClassMergeRuleService;
import com.onesports.intelligent.k12.polarlight.service.coach.CoachService;
import com.onesports.intelligent.k12.polarlight.service.common.CommonService;
import com.onesports.intelligent.k12.polarlight.service.course.CourseClassificationService;
import com.onesports.intelligent.k12.polarlight.service.course.CourseScheduleTemplateService;
import com.onesports.intelligent.k12.polarlight.service.course.CourseService;
import com.onesports.intelligent.k12.polarlight.service.course.SpellingClassDetailService;
import com.onesports.intelligent.k12.polarlight.service.course.manage.CourseSubLevelService;
import com.onesports.intelligent.k12.polarlight.service.operation.OrganizationService;
import com.onesports.intelligent.k12.polarlight.service.sys.MessageNotifyService;
import com.onesports.intelligent.k12.polarlight.service.sys.SysDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Array;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Wsy
 **/
@Api(tags = "通用下拉选项API")
@RestController
@RequiredArgsConstructor
public class DropDownController {

    private final OrganizationCampusService organizationCampusService;
    private final CourseService courseService;
    private final CoachService coachService;
    private final OrganizationService organizationService;
    private final CourseScheduleTemplateService courseScheduleTemplateService;

    private final SysDictService sysDictService;

    private final CommonService commonService;

    private final CourseSubLevelService courseSubLevelService;

    private final CourseClassificationService courseClassificationService;

    private final SpellingClassDetailService spellingClassDetailService;

    private final ClassMergeRuleService classMergeRuleService;
    private final MessageNotifyService messageNotifyService;

    private final CourseScheduleTemplateMapper courseScheduleTemplateMapper;
    @GetMapping("/campus")
    @ApiOperation("校区下拉")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "campusName", value = "校区名称", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "isContainUnUse", value = "是否只查可用校区 是：true 否 false", paramType = "query", dataType = "boolean")
    })
    public ResponseEntity<List<CanChoiceDataVO>> campus(@RequestParam(required = false) String campusName,
                                                        @RequestParam(required = false) Boolean isContainUnUse) {
        return ResultUtil.success(this.organizationCampusService.dropDown(campusName, isContainUnUse));
    }


    @GetMapping("/course")
    @ApiOperation("课程下拉")
    @ApiImplicitParam(name = "courseName", value = "课程名称", paramType = "query", dataType = "string")
    public ResponseEntity<List<CanChoiceDataVO>> course(@RequestParam(required = false) String courseName) {
        return ResultUtil.success(this.courseService.dropDown(courseName));
    }

    @GetMapping("/coach")
    @ApiOperation("教练下拉")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "coachName", value = "教练名称", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "isContainUnUse", value = "是否只查可用教练 是：true 否 false", paramType = "query", dataType = "boolean")
    })
    public ResponseEntity<List<CanChoiceDataVO>> course(@RequestParam(required = false) String coachName,
                                                        @RequestParam(required = false) Boolean isContainUnUse) {
        return ResultUtil.success(this.coachService.dropDown(coachName, isContainUnUse));
    }


    @GetMapping("/organization-campus")
    @ApiOperation("运营团队-校区联动下拉")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "teamName", value = "团队名称", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "provinceCode", value = "省编码", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "cityCode", value = "市编码", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "areaCode", value = "区编码", paramType = "query", dataType = "string")
    })
    public ResponseEntity<List<CanChoiceDataVO>> organizationCampus(@RequestParam(required = false) String teamName,
                                                                    @RequestParam(required = false) String provinceCode,
                                                                    @RequestParam(required = false) String cityCode,
                                                                    @RequestParam(required = false) String areaCode) {
        return ResultUtil.success(this.organizationService.dropDown(teamName, provinceCode, cityCode, areaCode));
    }


    @GetMapping("/schedule-template")
    @ApiOperation("上课时段列表")
    public ResponseEntity<List<String>> scheduleTemplate() {
        return ResultUtil.success(this.courseScheduleTemplateService.scheduleTemplate());
    }


    @GetMapping("/course-by-campusId")
    @ApiOperation("校区课程下拉")
    @ApiImplicitParam(name = "campusId", value = "校区id", paramType = "query", dataType = "string", required = true)
    public ResponseEntity<List<CampusCourseVO>> courseByCampusId(@RequestParam(required = false) String campusId) {
        return ResultUtil.success(this.courseService.courseByCampusId(campusId));
    }

    @GetMapping("/schedule-by-campusId")
    @ApiOperation("校区课表下拉")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "campusId", value = "校区id", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "courseId", value = "课程id", paramType = "query", dataType = "string")
    })

    public ResponseEntity<List<CampusScheduleVO>> scheduleByCampusId(String campusId, String courseId) {
        return ResultUtil.success(this.courseService.scheduleByCampusId(campusId, courseId));
    }

    @GetMapping("/Sections")
    @ApiOperation("节次下拉")
    public ResponseEntity<List<Dict>> getSections() {
        return ResultUtil.success(sysDictService.selectById("b551c56c701107e2e26aa977a4dccd95"));
    }

    @GetMapping("/SectionsBy")
    @ApiOperation("运营端新增专属节次下拉")
    public ResponseEntity<List<Dict>> getSectionsBy() {
        List<Dict> selectById = sysDictService.selectById("b551c56c701107e2e26aa977a4dccd95");
        List<String> codeList = courseScheduleTemplateMapper.selectList(null)
                .stream().map(CourseScheduleTemplate::getSectionCode).collect(Collectors.toList());
        List<Dict> dictList = selectById.stream().filter(i -> {
            List<String> collect = codeList.stream().filter(j -> j.equals(i.getCode())).collect(Collectors.toList());
            if(CollectionUtils.isNotEmpty(collect)){
                return false;
            }else {
                return true;
            }
        }).collect(Collectors.toList());
        return ResultUtil.success(dictList);
    }


    @ApiOperation("获取省市区")
    @GetMapping("/location")
    public ResponseEntity<List<LocationVO>> getLocation() {
        return ResultUtil.success(commonService.getLocation());
    }

    @ApiOperation("所有节次下拉")
    @GetMapping("/get-template")
    public ResponseEntity<List<TemplateVO>> getTemplate() {
        return ResultUtil.success(courseScheduleTemplateService.getTemplate());
    }


    @ApiOperation("根据编码获取省市区")
    @GetMapping("/locationByCode")
    @ApiImplicitParam(name = "regionCodes", value = "区域编码", paramType = "array", dataTypeClass = Array.class)
    public ResponseEntity<HashMap<String, String>> getLocation(@RequestParam(value = "regionCodes") List<String> regionCodes) {
        return ResultUtil.success(commonService.getLocationByCode(regionCodes));
    }

    @ApiOperation("课程分阶（授课类型）下拉")
    @GetMapping("/get-level-sub")
    public ResponseEntity<List<CourseSubLevel>> getSub() {
        return ResultUtil.success(courseSubLevelService.getSub());
    }

    @ApiOperation("机构获取当前班型名称下拉")
    @GetMapping("/spell-type-drop-down")
    public ResponseEntity<List<String>> getSpellType() {
        return ResultUtil.success(spellingClassDetailService.listBySpellType());
    }

    @ApiOperation("分类分阶对应关系下拉")
    @GetMapping("/classify-level")
    public ResponseEntity<List<CourseClassificationVO>> getClassifyLevel() {
        IPage<CourseClassificationVO> classificationList = courseClassificationService.getClassificationList(1L, Long.MAX_VALUE, null);
        List<CourseClassificationVO> records = classificationList.getRecords();
        return ResultUtil.success(records);
    }

    @ApiOperation("拼班规则列表")
    @GetMapping("/merge-rule")
    public ResponseEntity<List<CourseClassificationVO>> getMergeRule() {
        return ResultUtil.success(classMergeRuleService.list());
    }
}
